#!/usr/bin/env python

import sys
import random
import squarify # https://github.com/laserson/squarify
from PIL import Image, ImageDraw, ImageFont

width = 4*3840
height = 4*2160

# this is from survey_opcs
stats = {
	'abs.d': 256, 'abs.s': 1024, 'absq_s.ph': 1024, 'absq_s.qb': 1024,
	'absq_s.w': 1024, 'add': 32768, 'add.d': 4096, 'add.s': 32768, 'add_a.b':
	32768, 'add_a.d': 32768, 'add_a.h': 32768, 'add_a.w': 32768, 'addiu':
	67108864, 'addiupc': 16777216, 'addq.ph': 32768, 'addq_s.ph': 32768,
	'addq_s.w': 32768, 'addqh.ph': 32768, 'addqh.w': 32768, 'addqh_r.ph':
	32768, 'addqh_r.w': 32768, 'adds_a.b': 32768, 'adds_a.d': 32768,
	'adds_a.h': 32768, 'adds_a.w': 32768, 'adds_s.b': 32768, 'adds_s.d': 32768,
	'adds_s.h': 32768, 'adds_s.w': 32768, 'adds_u.b': 32768, 'adds_u.d': 32768,
	'adds_u.h': 32768, 'adds_u.w': 32768, 'addsc': 32768, 'addu': 31744,
	'addu.ph': 32768, 'addu.qb': 32768, 'addu_s.ph': 32768, 'addu_s.qb': 32768,
	'adduh.qb': 32768, 'adduh_r.qb': 32768, 'addv.b': 32768, 'addv.d': 32768,
	'addv.h': 32768, 'addv.w': 32768, 'addvi.b': 32768, 'addvi.d': 32768,
	'addvi.h': 32768, 'addvi.w': 32768, 'addwc': 32768, 'align': 131072,
	'aluipc': 2097152, 'and': 32768, 'and.v': 32768, 'andi': 67108864,
	'andi.b': 262144, 'append': 32768, 'asub_s.b': 32768, 'asub_s.d': 32768,
	'asub_s.h': 32768, 'asub_s.w': 32768, 'asub_u.b': 32768, 'asub_u.d': 32768,
	'asub_u.h': 32768, 'asub_u.w': 32768, 'aui': 67108864, 'auipc': 2097152,
	'ave_s.b': 32768, 'ave_s.d': 32768, 'ave_s.h': 32768, 'ave_s.w': 32768,
	'ave_u.b': 32768, 'ave_u.d': 32768, 'ave_u.h': 32768, 'ave_u.w': 32768,
	'aver_s.b': 32768, 'aver_s.d': 32768, 'aver_s.h': 32768, 'aver_s.w': 32768,
	'aver_u.b': 32768, 'aver_u.d': 32768, 'aver_u.h': 32768, 'aver_u.w': 32768,
	'b': 65536, 'bal': 65536, 'balc': 67108864, 'balign': 32768, 'bc':
	67108864, 'bc1eqz': 2097152, 'bc1nez': 2097152, 'bc2eqz': 2097152,
	'bc2nez': 2097152, 'bclr.b': 32768, 'bclr.d': 32768, 'bclr.h': 32768,
	'bclr.w': 32768, 'bclri.b': 8192, 'bclri.d': 65536, 'bclri.h': 16384,
	'bclri.w': 32768, 'beq': 65011712, 'beqc': 30474240, 'beql': 67043328,
	'beqz': 2031616, 'beqzalc': 2031616, 'beqzc': 65011712, 'beqzl': 65536,
	'bgec': 60948480, 'bgeuc': 60948480, 'bgez': 2097152, 'bgezalc': 2031616,
	'bgezall': 2097152, 'bgezc': 2031616, 'bgezl': 2097152, 'bgtz': 2097152,
	'bgtzalc': 2031616, 'bgtzc': 2031616, 'bgtzl': 2097152, 'binsl.b': 32768,
	'binsl.d': 32768, 'binsl.h': 32768, 'binsl.w': 32768, 'binsli.b': 8192,
	'binsli.d': 65536, 'binsli.h': 16384, 'binsli.w': 32768, 'binsr.b': 32768,
	'binsr.d': 32768, 'binsr.h': 32768, 'binsr.w': 32768, 'binsri.b': 8192,
	'binsri.d': 65536, 'binsri.h': 16384, 'binsri.w': 32768, 'bitrev': 1024,
	'bitswap': 1024, 'blez': 2097152, 'blezalc': 2031616, 'blezc': 2031616,
	'blezl': 2097152, 'bltc': 60948480, 'bltuc': 60948480, 'bltz': 2097152,
	'bltzalc': 2031616, 'bltzall': 2097152, 'bltzc': 2031616, 'bltzl': 2097152,
	'bmnz.v': 32768, 'bmnzi.b': 262144, 'bmz.v': 32768, 'bmzi.b': 262144,
	'bne': 65011712, 'bnec': 30474240, 'bneg.b': 32768, 'bneg.d': 32768,
	'bneg.h': 32768, 'bneg.w': 32768, 'bnegi.b': 8192, 'bnegi.d': 65536,
	'bnegi.h': 16384, 'bnegi.w': 32768, 'bnel': 65011712, 'bnez': 2097152,
	'bnezalc': 2031616, 'bnezc': 65011712, 'bnezl': 2097152, 'bnvc': 34603008,
	'bnz.b': 2097152, 'bnz.d': 2097152, 'bnz.h': 2097152, 'bnz.v': 2097152,
	'bnz.w': 2097152, 'bovc': 34603008, 'bposge32': 65536, 'break': 1048576,
	'bsel.v': 32768, 'bseli.b': 262144, 'bset.b': 32768, 'bset.d': 32768,
	'bset.h': 32768, 'bset.w': 32768, 'bseti.b': 8192, 'bseti.d': 65536,
	'bseti.h': 16384, 'bseti.w': 32768, 'bz.b': 2097152, 'bz.d': 2097152,
	'bz.h': 2097152, 'bz.v': 2097152, 'bz.w': 2097152, 'cache': 524288,
	'ceil.w.d': 512, 'ceil.w.s': 1024, 'ceq.b': 32768, 'ceq.d': 32768, 'ceq.h':
	32768, 'ceq.w': 32768, 'ceqi.b': 32768, 'ceqi.d': 32768, 'ceqi.h': 32768,
	'ceqi.w': 32768, 'cfc1': 1024, 'cfcmsa': 256, 'class.d': 1024, 'class.s':
	1024, 'cle_s.b': 32768, 'cle_s.d': 32768, 'cle_s.h': 32768, 'cle_s.w':
	32768, 'cle_u.b': 32768, 'cle_u.d': 32768, 'cle_u.h': 32768, 'cle_u.w':
	32768, 'clei_s.b': 32768, 'clei_s.d': 32768, 'clei_s.h': 32768, 'clei_s.w':
	32768, 'clei_u.b': 32768, 'clei_u.d': 32768, 'clei_u.h': 32768, 'clei_u.w':
	32768, 'clo': 1024, 'clt_s.b': 32768, 'clt_s.d': 32768, 'clt_s.h': 32768,
	'clt_s.w': 32768, 'clt_u.b': 32768, 'clt_u.d': 32768, 'clt_u.h': 32768,
	'clt_u.w': 32768, 'clti_s.b': 32768, 'clti_s.d': 32768, 'clti_s.h': 32768,
	'clti_s.w': 32768, 'clti_u.b': 32768, 'clti_u.d': 32768, 'clti_u.h': 32768,
	'clti_u.w': 32768, 'clz': 1024, 'cmp.af.d': 32768, 'cmp.af.s': 32768,
	'cmp.eq.d': 32768, 'cmp.eq.ph': 1024, 'cmp.eq.s': 32768, 'cmp.le.d': 32768,
	'cmp.le.ph': 1024, 'cmp.le.s': 32768, 'cmp.lt.d': 32768, 'cmp.lt.ph': 1024,
	'cmp.lt.s': 32768, 'cmp.saf.d': 32768, 'cmp.saf.s': 32768, 'cmp.seq.d':
	32768, 'cmp.seq.s': 32768, 'cmp.sle.d': 32768, 'cmp.sle.s': 32768,
	'cmp.slt.d': 32768, 'cmp.slt.s': 32768, 'cmp.sueq.d': 32768, 'cmp.sueq.s':
	32768, 'cmp.sule.d': 32768, 'cmp.sule.s': 32768, 'cmp.sult.d': 32768,
	'cmp.sult.s': 32768, 'cmp.sun.d': 32768, 'cmp.sun.s': 32768, 'cmp.ueq.d':
	32768, 'cmp.ueq.s': 32768, 'cmp.ule.d': 32768, 'cmp.ule.s': 32768,
	'cmp.ult.d': 32768, 'cmp.ult.s': 32768, 'cmp.un.d': 32768, 'cmp.un.s':
	32768, 'cmpgdu.eq.qb': 32768, 'cmpgdu.le.qb': 32768, 'cmpgdu.lt.qb': 32768,
	'cmpgu.eq.qb': 32768, 'cmpgu.le.qb': 32768, 'cmpgu.lt.qb': 32768,
	'cmpu.eq.qb': 1024, 'cmpu.le.qb': 1024, 'cmpu.lt.qb': 1024, 'copy_s.b':
	16384, 'copy_s.d': 2048, 'copy_s.h': 8192, 'copy_s.w': 4096, 'copy_u.b':
	16384, 'copy_u.d': 2048, 'copy_u.h': 8192, 'copy_u.w': 4096, 'ctc1': 1024,
	'ctcmsa': 256, 'cvt.d.s': 512, 'cvt.d.w': 512, 'cvt.l.d': 1024, 'cvt.l.s':
	1024, 'cvt.s.d': 512, 'cvt.s.w': 1024, 'cvt.w.d': 512, 'cvt.w.s': 1024,
	'deret': 1, 'di': 32, 'div': 32768, 'div.d': 4096, 'div.s': 32768,
	'div_s.b': 32768, 'div_s.d': 32768, 'div_s.h': 32768, 'div_s.w': 32768,
	'div_u.b': 32768, 'div_u.d': 32768, 'div_u.h': 32768, 'div_u.w': 32768,
	'divu': 32768, 'dlsa': 131072, 'dmfc1': 1024, 'dmtc1': 1024, 'dotp_s.d':
	32768, 'dotp_s.h': 32768, 'dotp_s.w': 32768, 'dotp_u.d': 32768, 'dotp_u.h':
	32768, 'dotp_u.w': 32768, 'dpa.w.ph': 4096, 'dpadd_s.d': 32768,
	'dpadd_s.h': 32768, 'dpadd_s.w': 32768, 'dpadd_u.d': 32768, 'dpadd_u.h':
	32768, 'dpadd_u.w': 32768, 'dpaq_s.w.ph': 4096, 'dpaq_sa.l.w': 4096,
	'dpaqx_s.w.ph': 4096, 'dpaqx_sa.w.ph': 4096, 'dpau.h.qbl': 4096,
	'dpau.h.qbr': 4096, 'dpax.w.ph': 4096, 'dps.w.ph': 4096, 'dpsq_s.w.ph':
	4096, 'dpsq_sa.l.w': 4096, 'dpsqx_s.w.ph': 4096, 'dpsqx_sa.w.ph': 4096,
	'dpsu.h.qbl': 4096, 'dpsu.h.qbr': 4096, 'dpsub_s.d': 32768, 'dpsub_s.h':
	32768, 'dpsub_s.w': 32768, 'dpsub_u.d': 32768, 'dpsub_u.h': 32768,
	'dpsub_u.w': 32768, 'dpsx.w.ph': 4096, 'ehb': 1, 'ei': 32, 'eret': 1,
	'ext': 1048576, 'extp': 4096, 'extpdp': 4096, 'extpdpv': 4096, 'extpv':
	4096, 'extr.w': 4096, 'extr_r.w': 4096, 'extr_rs.w': 4096, 'extr_s.h':
	4096, 'extrv.w': 4096, 'extrv_r.w': 4096, 'extrv_rs.w': 4096, 'extrv_s.h':
	4096, 'fadd.d': 32768, 'fadd.w': 32768, 'fcaf.d': 32768, 'fcaf.w': 32768,
	'fceq.d': 32768, 'fceq.w': 32768, 'fclass.d': 1024, 'fclass.w': 1024,
	'fcle.d': 32768, 'fcle.w': 32768, 'fclt.d': 32768, 'fclt.w': 32768,
	'fcne.d': 32768, 'fcne.w': 32768, 'fcor.d': 32768, 'fcor.w': 32768,
	'fcueq.d': 32768, 'fcueq.w': 32768, 'fcule.d': 32768, 'fcule.w': 32768,
	'fcult.d': 32768, 'fcult.w': 32768, 'fcun.d': 32768, 'fcun.w': 32768,
	'fcune.d': 32768, 'fcune.w': 32768, 'fdiv.d': 32768, 'fdiv.w': 32768,
	'fexdo.h': 32768, 'fexdo.w': 32768, 'fexp2.d': 32768, 'fexp2.w': 32768,
	'fexupl.d': 1024, 'fexupl.w': 1024, 'fexupr.d': 1024, 'fexupr.w': 1024,
	'ffint_s.d': 1024, 'ffint_s.w': 1024, 'ffint_u.d': 1024, 'ffint_u.w': 1024,
	'ffql.d': 1024, 'ffql.w': 1024, 'ffqr.d': 1024, 'ffqr.w': 1024, 'fill.b':
	1024, 'fill.d': 1024, 'fill.h': 1024, 'fill.w': 1024, 'flog2.d': 1024,
	'flog2.w': 1024, 'floor.w.d': 512, 'floor.w.s': 1024, 'fmadd.d': 32768,
	'fmadd.w': 32768, 'fmax.d': 32768, 'fmax.w': 32768, 'fmax_a.d': 32768,
	'fmax_a.w': 32768, 'fmin.d': 32768, 'fmin.w': 32768, 'fmin_a.d': 32768,
	'fmin_a.w': 32768, 'fmsub.d': 32768, 'fmsub.w': 32768, 'fmul.d': 32768,
	'fmul.w': 32768, 'frcp.d': 1024, 'frcp.w': 1024, 'frint.d': 1024,
	'frint.w': 1024, 'frsqrt.d': 1024, 'frsqrt.w': 1024, 'fsaf.d': 32768,
	'fsaf.w': 32768, 'fseq.d': 32768, 'fseq.w': 32768, 'fsle.d': 32768,
	'fsle.w': 32768, 'fslt.d': 32768, 'fslt.w': 32768, 'fsne.d': 32768,
	'fsne.w': 32768, 'fsor.d': 32768, 'fsor.w': 32768, 'fsqrt.d': 1024,
	'fsqrt.w': 1024, 'fsub.d': 32768, 'fsub.w': 32768, 'fsueq.d': 32768,
	'fsueq.w': 32768, 'fsule.d': 32768, 'fsule.w': 32768, 'fsult.d': 32768,
	'fsult.w': 32768, 'fsun.d': 32768, 'fsun.w': 32768, 'fsune.d': 32768,
	'fsune.w': 32768, 'ftint_s.d': 1024, 'ftint_s.w': 1024, 'ftint_u.d': 1024,
	'ftint_u.w': 1024, 'ftq.h': 32768, 'ftq.w': 32768, 'ftrunc_s.d': 1024,
	'ftrunc_s.w': 1024, 'ftrunc_u.d': 1024, 'ftrunc_u.w': 1024, 'hadd_s.d':
	32768, 'hadd_s.h': 32768, 'hadd_s.w': 32768, 'hadd_u.d': 32768, 'hadd_u.h':
	32768, 'hadd_u.w': 32768, 'hsub_s.d': 32768, 'hsub_s.h': 32768, 'hsub_s.w':
	32768, 'hsub_u.d': 32768, 'hsub_u.h': 32768, 'hsub_u.w': 32768, 'ilvev.b':
	32768, 'ilvev.d': 32768, 'ilvev.h': 32768, 'ilvev.w': 32768, 'ilvl.b':
	32768, 'ilvl.d': 32768, 'ilvl.h': 32768, 'ilvl.w': 32768, 'ilvod.b': 32768,
	'ilvod.d': 32768, 'ilvod.h': 32768, 'ilvod.w': 32768, 'ilvr.b': 32768,
	'ilvr.d': 32768, 'ilvr.h': 32768, 'ilvr.w': 32768, 'ins': 1048576,
	'insert.b': 16384, 'insert.d': 2048, 'insert.h': 8192, 'insert.w': 4096,
	'insv': 1024, 'insve.b': 16384, 'insve.d': 2048, 'insve.h': 8192,
	'insve.w': 4096, 'j': 67108864, 'jal': 67108864, 'jalr': 992, 'jalr.hb':
	992, 'jialc': 2097152, 'jic': 2097152, 'jr': 64, 'jr.hb': 32, 'lb':
	67108864, 'lbu': 67108864, 'lbux': 32768, 'ld.b': 1048576, 'ld.d': 1048576,
	'ld.h': 1048576, 'ld.w': 1048576, 'ldc1': 67108864, 'ldc2': 2097152,
	'ldi.b': 32768, 'ldi.d': 32768, 'ldi.h': 32768, 'ldi.w': 32768, 'lh':
	67108864, 'lhu': 67108864, 'lhx': 32768, 'll': 1048576, 'lld': 1048576,
	'lsa': 131072, 'lw': 67108864, 'lwc1': 67108864, 'lwc2': 2097152, 'lwpc':
	16777216, 'lwupc': 16777216, 'lwx': 32768, 'madd': 4096, 'madd_q.h': 32768,
	'madd_q.w': 32768, 'maddf.d': 32768, 'maddf.s': 32768, 'maddr_q.h': 32768,
	'maddr_q.w': 32768, 'maddu': 4096, 'maddv.b': 32768, 'maddv.d': 32768,
	'maddv.h': 32768, 'maddv.w': 32768, 'maq_s.w.phl': 4096, 'maq_s.w.phr':
	4096, 'maq_sa.w.phl': 4096, 'maq_sa.w.phr': 4096, 'max.d': 32768, 'max.s':
	32768, 'max_a.b': 32768, 'max_a.d': 32768, 'max_a.h': 32768, 'max_a.w':
	32768, 'max_s.b': 32768, 'max_s.d': 32768, 'max_s.h': 32768, 'max_s.w':
	32768, 'max_u.b': 32768, 'max_u.d': 32768, 'max_u.h': 32768, 'max_u.w':
	32768, 'maxa.d': 32768, 'maxa.s': 32768, 'maxi_s.b': 32768, 'maxi_s.d':
	32768, 'maxi_s.h': 32768, 'maxi_s.w': 32768, 'maxi_u.b': 32768, 'maxi_u.d':
	32768, 'maxi_u.h': 32768, 'maxi_u.w': 32768, 'mfc0': 8192, 'mfc1': 1024,
	'mfc2': 8192, 'mfhc1': 512, 'mfhi': 128, 'mflo': 128, 'min.d': 32768,
	'min.s': 32768, 'min_a.b': 32768, 'min_a.d': 32768, 'min_a.h': 32768,
	'min_a.w': 32768, 'min_s.b': 32768, 'min_s.d': 32768, 'min_s.h': 32768,
	'min_s.w': 32768, 'min_u.b': 32768, 'min_u.d': 32768, 'min_u.h': 32768,
	'min_u.w': 32768, 'mina.d': 32768, 'mina.s': 32768, 'mini_s.b': 32768,
	'mini_s.d': 32768, 'mini_s.h': 32768, 'mini_s.w': 32768, 'mini_u.b': 32768,
	'mini_u.d': 32768, 'mini_u.h': 32768, 'mini_u.w': 32768, 'mod': 32768,
	'mod_s.b': 32768, 'mod_s.d': 32768, 'mod_s.h': 32768, 'mod_s.w': 32768,
	'mod_u.b': 32768, 'mod_u.d': 32768, 'mod_u.h': 32768, 'mod_u.w': 32768,
	'modsub': 32768, 'modu': 32768, 'mov.d': 256, 'mov.s': 1024, 'move': 2048,
	'move.v': 1024, 'msub': 4096, 'msub_q.h': 32768, 'msub_q.w': 32768,
	'msubf.d': 32768, 'msubf.s': 32768, 'msubr_q.h': 32768, 'msubr_q.w': 32768,
	'msubu': 4096, 'msubv.b': 32768, 'msubv.d': 32768, 'msubv.h': 32768,
	'msubv.w': 32768, 'mtc0': 8192, 'mtc1': 1024, 'mtc2': 8192, 'mthc1': 512,
	'mthi': 128, 'mthlip': 128, 'mtlo': 128, 'muh': 32768, 'muhu': 32768,
	'mul': 32768, 'mul.d': 4096, 'mul.ph': 32768, 'mul.s': 32768, 'mul_q.h':
	32768, 'mul_q.w': 32768, 'mul_s.ph': 32768, 'muleq_s.w.phl': 32768,
	'muleq_s.w.phr': 32768, 'muleu_s.ph.qbl': 32768, 'muleu_s.ph.qbr': 32768,
	'mulq_rs.ph': 32768, 'mulq_rs.w': 32768, 'mulq_s.ph': 32768, 'mulq_s.w':
	32768, 'mulr_q.h': 32768, 'mulr_q.w': 32768, 'mulsa.w.ph': 4096,
	'mulsaq_s.w.ph': 4096, 'mult': 4096, 'multu': 4096, 'mulu': 32768,
	'mulv.b': 32768, 'mulv.d': 32768, 'mulv.h': 32768, 'mulv.w': 32768, 'neg':
	1024, 'neg.d': 256, 'neg.s': 1024, 'negu': 1024, 'nloc.b': 1024, 'nloc.d':
	1024, 'nloc.h': 1024, 'nloc.w': 1024, 'nlzc.b': 1024, 'nlzc.d': 1024,
	'nlzc.h': 1024, 'nlzc.w': 1024, 'nop': 1, 'nor': 31744, 'nor.v': 32768,
	'nori.b': 262144, 'not': 1024, 'or': 31744, 'or.v': 32768, 'ori': 67108864,
	'ori.b': 262144, 'packrl.ph': 32768, 'pause': 1, 'pckev.b': 32768,
	'pckev.d': 32768, 'pckev.h': 32768, 'pckev.w': 32768, 'pckod.b': 32768,
	'pckod.d': 32768, 'pckod.h': 32768, 'pckod.w': 32768, 'pcnt.b': 1024,
	'pcnt.d': 1024, 'pcnt.h': 1024, 'pcnt.w': 1024, 'pick.ph': 32768,
	'pick.qb': 32768, 'preceq.w.phl': 1024, 'preceq.w.phr': 1024,
	'precequ.ph.qbl': 1024, 'precequ.ph.qbla': 1024, 'precequ.ph.qbr': 1024,
	'precequ.ph.qbra': 1024, 'preceu.ph.qbl': 1024, 'preceu.ph.qbla': 1024,
	'preceu.ph.qbr': 1024, 'preceu.ph.qbra': 1024, 'precr.qb.ph': 32768,
	'precr_sra.ph.w': 32768, 'precr_sra_r.ph.w': 32768, 'precrq.ph.w': 32768,
	'precrq.qb.ph': 32768, 'precrq_rs.ph.w': 32768, 'precrqu_s.qb.ph': 32768,
	'pref': 524288, 'prepend': 32768, 'raddu.w.qb': 1024, 'rddsp': 32768,
	'rdhwr': 32, 'repl.ph': 32768, 'repl.qb': 32768, 'replv.ph': 1024,
	'replv.qb': 1024, 'rint.d': 1024, 'rint.s': 1024, 'rotr': 32768, 'rotrv':
	32768, 'round.w.d': 512, 'round.w.s': 1024, 'sat_s.b': 8192, 'sat_s.d':
	65536, 'sat_s.h': 16384, 'sat_s.w': 32768, 'sat_u.b': 8192, 'sat_u.d':
	65536, 'sat_u.h': 16384, 'sat_u.w': 32768, 'sb': 67108864, 'sc': 1048576,
	'scd': 1048576, 'sdbbp': 1048576, 'sdc1': 67108864, 'sdc2': 2097152, 'seb':
	1024, 'seh': 1024, 'sel.d': 32768, 'sel.s': 32768, 'seleqz.d': 32768,
	'seleqz.s': 32768, 'selnez.d': 32768, 'selnez.s': 32768, 'sh': 67108864,
	'shf.b': 262144, 'shf.h': 262144, 'shf.w': 262144, 'shilo': 256, 'shilov':
	128, 'shll.ph': 32768, 'shll.qb': 32768, 'shll_s.ph': 32768, 'shll_s.w':
	32768, 'shllv.ph': 32768, 'shllv.qb': 32768, 'shllv_s.ph': 32768,
	'shllv_s.w': 32768, 'shra.ph': 32768, 'shra.qb': 32768, 'shra_r.ph': 32768,
	'shra_r.qb': 32768, 'shra_r.w': 32768, 'shrav.ph': 32768, 'shrav.qb':
	32768, 'shrav_r.ph': 32768, 'shrav_r.qb': 32768, 'shrav_r.w': 32768,
	'shrl.ph': 32768, 'shrl.qb': 32768, 'shrlv.ph': 32768, 'shrlv.qb': 32768,
	'sld.b': 32768, 'sld.d': 32768, 'sld.h': 32768, 'sld.w': 32768, 'sldi.b':
	16384, 'sldi.d': 2048, 'sldi.h': 8192, 'sldi.w': 4096, 'sll': 32764,
	'sll.b': 32768, 'sll.d': 32768, 'sll.h': 32768, 'sll.w': 32768, 'slli.b':
	8192, 'slli.d': 65536, 'slli.h': 16384, 'slli.w': 32768, 'sllv': 32768,
	'slt': 32768, 'slti': 67108864, 'sltiu': 67108864, 'sltu': 32768,
	'splat.b': 32768, 'splat.d': 32768, 'splat.h': 32768, 'splat.w': 32768,
	'splati.b': 16384, 'splati.d': 2048, 'splati.h': 8192, 'splati.w': 4096,
	'sqrt.d': 256, 'sqrt.s': 1024, 'sra': 32768, 'sra.b': 32768, 'sra.d':
	32768, 'sra.h': 32768, 'sra.w': 32768, 'srai.b': 8192, 'srai.d': 65536,
	'srai.h': 16384, 'srai.w': 32768, 'srar.b': 32768, 'srar.d': 32768,
	'srar.h': 32768, 'srar.w': 32768, 'srari.b': 8192, 'srari.d': 65536,
	'srari.h': 16384, 'srari.w': 32768, 'srav': 32768, 'srl': 32768, 'srl.b':
	32768, 'srl.d': 32768, 'srl.h': 32768, 'srl.w': 32768, 'srli.b': 8192,
	'srli.d': 65536, 'srli.h': 16384, 'srli.w': 32768, 'srlr.b': 32768,
	'srlr.d': 32768, 'srlr.h': 32768, 'srlr.w': 32768, 'srlri.b': 8192,
	'srlri.d': 65536, 'srlri.h': 16384, 'srlri.w': 32768, 'srlv': 32768,
	'ssnop': 1, 'st.b': 1048576, 'st.d': 1048576, 'st.h': 1048576, 'st.w':
	1048576, 'sub': 31744, 'sub.d': 4096, 'sub.s': 32768, 'subq.ph': 32768,
	'subq_s.ph': 32768, 'subq_s.w': 32768, 'subqh.ph': 32768, 'subqh.w': 32768,
	'subqh_r.ph': 32768, 'subqh_r.w': 32768, 'subs_s.b': 32768, 'subs_s.d':
	32768, 'subs_s.h': 32768, 'subs_s.w': 32768, 'subs_u.b': 32768, 'subs_u.d':
	32768, 'subs_u.h': 32768, 'subs_u.w': 32768, 'subsus_u.b': 32768,
	'subsus_u.d': 32768, 'subsus_u.h': 32768, 'subsus_u.w': 32768,
	'subsuu_s.b': 32768, 'subsuu_s.d': 32768, 'subsuu_s.h': 32768,
	'subsuu_s.w': 32768, 'subu': 31744, 'subu.ph': 32768, 'subu.qb': 32768,
	'subu_s.ph': 32768, 'subu_s.qb': 32768, 'subuh.qb': 32768, 'subuh_r.qb':
	32768, 'subv.b': 32768, 'subv.d': 32768, 'subv.h': 32768, 'subv.w': 32768,
	'subvi.b': 32768, 'subvi.d': 32768, 'subvi.h': 32768, 'subvi.w': 32768,
	'sw': 67108864, 'swc1': 67108864, 'swc2': 2097152, 'sync': 1048576,
	'syscall': 1048576, 'teq': 1048576, 'tge': 1048576, 'tgeu': 1048576,
	'tlbp': 1, 'tlbr': 1, 'tlbwi': 1, 'tlbwr': 1, 'tlt': 1048576, 'tltu':
	1048576, 'tne': 1048576, 'trunc.w.d': 512, 'trunc.w.s': 1024, 'undef':
	1792062841, 'vshf.b': 32768, 'vshf.d': 32768, 'vshf.h': 32768, 'vshf.w':
	32768, 'wait': 1, 'wrdsp': 32768, 'wsbh': 1024, 'xor': 32768, 'xor.v':
	32768, 'xori': 67108864, 'xori.b': 262144,
}

# palette
# there are 453 opcodes after filtering
# palette generated from https://github.com/taketwo/glasbey
# `./glasbey.py --no-black --format byte 23 output.txt`
palette = [
(0,0,0), (0,0,143), (182,0,0), (0,140,0), (195,79,255), (1,165,202),
(236,157,0), (118,255,0), (89,83,84), (255,117,152), (148,0,115), (0,243,204),
(72,83,255), (166,161,154), (0,67,1), (237,183,255), (138,104,0), (97,0,163),
(92,0,17), (255,245,133), (0,123,105), (146,184,83), (171,212,255),
(126,121,163), (255,84,1), (10,87,125), (168,97,92), (231,0,185),
(255,195,166), (91,53,0), (0,180,133), (126,158,255), (231,2,92),
(184,216,183), (192,130,183), (111,137,91), (138,72,162), (91,50,90),
(220,138,103), (79,92,44), (0,225,115), (255,104,255), (126,193,193),
(120,58,61), (183,252,255), (136,9,255), (111,140,144), (172,168,204),
(148,67,5), (0,80,74), (1,0,250), (86,81,148), (202,216,0), (9,123,192),
(176,255,155), (173,147,75), (196,104,0), (218,203,215), (0,186,0),
(173,82,137), (131,119,114), (156,0,59), (144,110,254), (213,195,115),
(254,172,203), (0,233,255), (193,146,255), (130,142,0), (100,0,65),
(87,104,120), (125,94,120), (0,113,52), (122,90,59), (53,46,119), (209,72,57),
(87,0,227), (138,178,143), (190,0,205), (130,23,0), (244,234,193),
(15,131,149), (255,201,23), (184,134,140), (72,198,255), (89,109,95),
(194,0,118), (253,255,0), (67,72,95), (123,153,189), (117,0,132), (88,114,2),
(78,163,79), (195,175,0), (9,162,157), (189,212,217), (171,255,214),
(126,210,0), (209,90,119), (255,0,34), (255,92,194), (161,114,68),
(73,128,255), (160,109,188), (215,209,255), (163,59,54), (200,165,188),
(255,132,43), (155,141,160), (203,191,166), (208,90,206), (5,86,184),
(254,92,89), (116,212,162), (104,107,196), (132,63,102), (0,161,252),
(9,147,99), (239,148,221), (30,85,50), (182,217,117), (168,0,239),
(254,171,99), (255,159,150), (151,169,178), (255,22,149), (0,103,112),
(151,142,212), (105,80,0), (82,255,170), (170,184,255), (60,67,0), (85,72,45),
(47,91,0), (80,0,108), (255,216,255), (99,37,17), (0,59,113), (210,163,152),
(99,155,132), (0,206,180), (103,75,122), (151,224,209), (160,114,143),
(135,89,202), (220,255,199), (224,236,255), (94,126,161), (173,174,123),
(148,141,110), (114,111,62), (133,2,190), (195,252,83), (255,226,223),
(66,85,88), (213,242,229), (0,195,215), (220,140,170), (165,57,93), (193,9,57),
(130,87,89), (51,0,187), (188,72,0), (199,106,71), (154,0,162), (68,101,151),
(198,133,0), (141,183,217), (172,188,179), (208,164,110), (209,101,164),
(201,158,218), (96,193,112), (119,25,52), (251,0,244), (116,169,5),
(80,51,113), (69,133,62), (103,70,86), (211,126,233), (132,235,100),
(180,36,152), (139,156,81), (119,122,133), (89,255,249), (152,222,239),
(255,217,138), (118,64,33), (220,121,124), (106,166,178), (67,82,65),
(112,135,206), (107,57,181), (159,130,4), (103,98,134), (216,217,208),
(78,119,120), (181,136,107), (171,82,173), (183,179,187), (202,178,254),
(118,81,253), (185,195,220), (65,0,126), (48,99,84), (57,64,161), (250,75,121),
(120,134,121), (160,93,13), (217,27,0), (255,125,90), (0,234,41), (100,44,67),
(152,200,135), (122,56,124), (234,187,191), (224,216,92), (146,80,58),
(219,227,153), (82,131,105), (213,62,140), (1,111,148), (206,185,221),
(131,99,152), (219,6,251), (94,2,91), (75,113,70), (171,118,235),
(137,157,149), (72,145,198), (166,191,0), (0,75,91), (90,75,202),
(159,232,169), (166,136,188), (221,144,65), (96,94,72), (116,35,94),
(139,131,66), (91,178,161), (204,166,67), (105,73,64), (109,102,109),
(122,182,254), (165,156,36), (191,66,83), (34,115,0), (227,109,39),
(70,225,217), (155,85,107), (155,0,24), (57,77,125), (92,84,103),
(135,144,160), (143,80,135), (141,34,84), (245,233,248), (254,131,193),
(54,57,232), (108,53,143), (76,9,172), (241,184,76), (165,76,255),
(183,109,127), (90,148,27), (160,119,111), (0,111,235), (158,43,0),
(0,255,119), (122,120,19), (84,144,166), (167,159,255), (228,116,206),
(193,125,65), (136,117,200), (136,41,41), (80,203,71), (63,140,130),
(130,81,18), (234,152,255), (114,161,109), (155,138,138), (146,199,178),
(134,116,141), (178,82,56), (102,100,20), (237,188,226), (213,244,131),
(148,164,219), (246,225,9), (224,18,53), (114,83,159), (44,0,137),
(186,192,84), (199,72,169), (95,168,219), (188,200,147), (222,71,2),
(164,231,1), (123,132,255), (168,79,208), (123,0,23), (172,116,23),
(243,255,230), (134,112,83), (140,107,117), (227,87,255), (0,204,139),
(0,164,34), (117,201,227), (138,209,102), (255,35,81), (228,189,139),
(255,207,225), (179,105,164), (68,98,194), (0,55,159), (160,192,204),
(236,166,131), (254,255,185), (180,158,127), (79,57,145), (62,88,109),
(0,232,163), (159,52,118), (81,65,95), (190,118,108), (238,87,154), (0,185,78),
(108,136,54), (120,45,0), (117,98,89), (82,67,11), (150,147,183), (220,95,93),
(149,161,132), (33,125,84), (235,210,185), (253,148,171), (102,12,0),
(193,235,255), (185,55,31), (253,131,130), (0,160,131), (146,68,77),
(219,197,10), (180,154,161), (182,11,83), (100,111,160), (7,184,182),
(130,235,192), (221,114,149), (91,98,99), (233,92,58), (112,118,94),
(202,180,179), (227,162,174), (78,120,204), (103,188,148), (0,139,231),
(178,157,223), (136,177,167), (221,123,0), (160,40,191), (82,83,0),
(215,33,208), (138,101,46), (204,46,105), (110,116,114), (71,117,138),
(178,154,187), (102,39,43), (137,55,206), (137,140,141), (145,100,84),
(57,77,38), (64,107,36), (0,150,62), (212,216,178), (68,64,112), (110,19,255),
(254,150,100), (105,109,255), (161,159,168), (234,75,201), (160,134,254),
(220,250,255), (179,139,164), (189,106,216), (168,172,86), (0,76,143),
(103,91,44), (94,37,111), (39,71,100), (222,65,87), (145,175,113),
(167,214,68), (182,79,113), (126,255,226), (9,135,177), (223,145,139),
(104,72,28), (105,180,73), (119,221,138), (212,150,195), (195,123,157),
(227,176,0), (116,74,110), (210,213,228), (255,173,241), (166,0,101),
(141,59,35), (80,87,120), (96,117,52), (100,55,43), (157,59,153), (70,163,110),
(197,199,201), (118,104,169), (95,180,209), (0,103,67), (227,233,235),
(98,0,41), (153,169,0), (163,95,55), (241,255,104), (225,116,90),
(195,238,164), (194,10,251), (173,177,154), (90,147,238), (1,0,188),
(255,38,185), (63,96,65), (96,156,152), (154,118,163), (25,41,126), (3,85,24),
(207,1,153), (150,166,194), (203,220,255), (189,187,235), (166,194,161),
(0,154,170), (184,134,219), (0,121,123), (192,151,5), (0,104,173),
(130,37,153), (0,213,251), (196,240,203), (100,0,201), (126,0,82),
(129,190,43)]

sum_ = 0
for opc in stats:
	sum_ += stats[opc]

print '%d opcodes total' % len(stats)
print 'total: %d (should be %d)' % (sum_, 0x100000000)

# let's drop the ".stuff" suffix on opcodes
stats2 = {}
for opc in stats:
	opc2 = opc
	
	idx = opc.find('.')
	if idx != -1:
		opc2 = opc[0:idx]

	if opc2 in stats2:
		stats2[opc2] += stats[opc]
	else:
		stats2[opc2] = stats[opc] 

# draw the filtered list
sum_ = 0
for opc2 in sorted(stats2):
	sum_ += stats2[opc2]
	#print '%s appears %d times' % (opc2, stats2[opc2])

print '%d opcodes total' % len(stats2)
print 'total: %d (should be %d)' % (sum_, 0x100000000)

# these values define the coordinate system for the returned rectangles
# the values will range from x to x + width and y to y + height
x = 0
y = 0

# values must be sorted descending (and positive, obviously)
opcs = sorted(list(stats2), key=lambda x: stats2[x], reverse=True)

print 'with the most encodings:'
for opc in opcs[0:8]:
	print '    %s: %d' % (opc, stats2[opc])
print 'with the least encodings:'
for opc in opcs[-8:]:
	print '    %s: %d' % (opc, stats2[opc])

values = map(lambda x: stats2[x], opcs)

# the sum of the values must equal the total area to be laid out
# i.e., sum(values) == width * height
normalized = squarify.normalize_sizes(values, width, height)

# returns a list of rectangles
rects = squarify.squarify(normalized, x, y, width, height)
#rects = squarify.padded_squarify(normalized, x, y, width, height)

# draw this thing
img = Image.new('RGB', (width, height))
draw = ImageDraw.Draw(img)
for (idx,rect) in enumerate(rects):
	x0 = rect['x']
	y0 = rect['y']
	w = rect['dx']
	h = rect['dy']
	xc = x0 + w/2
	yc = y0 + h/2

	bgcolor = palette[idx]
	draw.rectangle([(x0,y0), (x0+w),(y0+h)], fill=bgcolor)

	string = opcs[idx].upper()
	#print "drawing text \"%s\" at (%f,%f)" % (string, xc, yc)
	draw.text((xc,yc), string)
del draw
img.save("/tmp/tmp.png")

